// pages/comment-detail/comment-detail.js
var postData = require('../../data/posts-data.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    wannerTalk: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var _this = this
    var id = options.id
    var openid = wx.getStorageSync('openid')
    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/talk/sub',
      data: {
        id: id,
        openid: openid
      },
      method: 'POST',
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        console.log(res)
        if (res.data.code == 200) {
          _this.setData({
            comments_list: res.data.data
          })
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })

  },
  onComZanTap: function (e) {
    var comIndex = e.currentTarget.dataset.index
    var id = e.currentTarget.dataset.id
    var openid = wx.getStorageSync('openid')
    var all_comments = this.data.comments_list.last
    // console.log(this.data.all_comments.data[comIndex].is_good)
    var _this = this
    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/talk/good?id=' + id + '&openid=' + openid,
      data: {},
      method: 'GET',
      success: function (res) {
        // success
        console.log(res)
        all_comments[comIndex].is_good = !(all_comments[comIndex].is_good)
        console.log(all_comments)
        _this.setData({
          'comments_list.last': all_comments
        })
        if (res.data.code == 200) {
          console.log('coll')
          _this.setData({
            'comments_list.last': all_comments
          })
          if (all_comments[comIndex].is_good) {
            all_comments[comIndex].good_count = all_comments[comIndex].good_count + 1
            _this.setData({
              'comments_list.last': all_comments
            })
          } else {
            all_comments[comIndex].good_count = all_comments[comIndex].good_count - 1
            _this.setData({
              'comments_list.last': all_comments
            })
          }
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  replyTap: function (e) {
    console.log('cool')
    console.log(e.currentTarget.dataset)
    var openid = wx.getStorageSync('openid')
    var dataset = e.currentTarget.dataset
    var res_id = dataset.res_id
    var parent_id = dataset.parent_id
    var target_talk_id = dataset.target_talk_id
    var target_user_id = dataset.target_user_id
    var target_talk_headimgurl = dataset.target_talk_headimgurl
    var target_talk_nickname = dataset.target_talk_nickname
    this.setData({
      current_reply_data: {
        openid: openid,
        res_id: res_id,
        parent_id: parent_id,
        target_talk_id: target_talk_id,
        target_user_id: target_user_id,
        target_talk_nickname: target_talk_nickname,
        target_talk_headimgurl: target_talk_headimgurl
      },
      wannerTalk: true
    })

  },
  getTalk: function (e) {
    var val = e.detail.value
    console.log(val)
    this.setData({
      val: val
    })
  },
  submitComment: function (e) {
    var _this = this
    var content = _this.data.val
    console.log(content)
    var userinfo = wx.getStorageSync('userInfo')
    console.log(userinfo)
    // var id = this.data.post_detail.id
    var current_reply_data = _this.data.current_reply_data
    var comments = _this.data.comments_list.last
    var sonComment = _this.data.comments_list.parent
    var openid = wx.getStorageSync('openid')
    var parData = {}
    console.log(openid)
    if (current_reply_data) {
      parData = {
        openid: current_reply_data.openid,
        res_id: current_reply_data.res_id,
        parent_id: current_reply_data.parent_id,
        target_talk_id: current_reply_data.target_talk_id,
        target_user_id: current_reply_data.target_user_id,
        content: '回复@ ' + current_reply_data.target_talk_nickname + '：' + content
      }
    } else {
      parData = {
        openid: openid,
        res_id: sonComment.res_id,
        parent_id: sonComment.id,
        target_talk_id: sonComment.id,
        target_user_id: sonComment.user_id,
        content: content
      }
    }
    _this.setData({
      parData: parData
    })


    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/talk/add',
      data: parData,
      method: 'POST',
      success: function (res) {
        // success
        console.log(res.data.data)
        var new_comment = {
          res_id: _this.data.parData.res_id,
          parent_id: _this.data.parData.parent_id,
          target_talk_id: _this.data.parData.target_talk_id,
          target_user_id: _this.data.parData.target_user_id,
          content: _this.data.parData.content,
          target_talk_nickname: _this.data.parData.target_talk_nickname,
          nickname: userinfo.nickName,
          headimgurl: userinfo.avatarUrl,
          have_del: true,
          is_good: false,
          good_count: 0
        }
        comments.unshift(new_comment)
        _this.setData({
          wannerTalk: false,
          'comments_list.last': comments
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  delComment: function (e) {
    console.log(123)
    var _this = this
    var id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    var openid = wx.getStorageSync('openid')
    console.log(openid)
    var all_comments = this.data.comments_list.last
    wx.showModal({
      title: '删除评论',
      content: '请确认是否删除评论',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: 'https://shipin.lekuaiwang.com/api/talk/delete?id=' + id + '&openid=' + openid,
            method: "POST",
            data: {
            },
            header: {
              "content-type": "application/x-www-form-urlencoded;charset=utf-8",
            },
            success: res => {
              all_comments.splice(index, 1)
              _this.setData({
                'comments_list.last': all_comments
              })
              wx.showToast({
                title: "删除成功"
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  onReplyTap: function (e) {
    console.log(900)
    console.log(e)
    var commentid = e.currentTarget.dataset.commentid
    wx.navigateTo({
      url: '/pages/comment-detail/comment-detail?id=' + commentid
    })
  },
  exposeTap: function (e) {
    console.log(e.currentTarget.dataset)
    var dataset = e.currentTarget.dataset
    this.setData({
      isExpose: true,
      expose_talk: {
        target_user_id: dataset.talk_user_id,
        talk_id: dataset.id,
      }
    })
  },
  toggleCheck: function (e) {
    console.log(e.currentTarget.dataset.content)
    this.setData({
      _num: e.currentTarget.dataset.num,
      _reason: e.currentTarget.dataset.content
    })
  },
  getReason: function (e) {
    var val = e.detail.value
    console.log(val)
    this.setData({
      reason_val: val
    })
  },
  exposeFun: function (e) {
    var _this = this
    var openid = wx.getStorageSync('openid')
    this.setData({
      isExpose: false,
    })
    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/talk/report',
      data: {
        openid: openid,
        target_user_id: _this.data.expose_talk.talk_user_id,
        talk_id: _this.data.expose_talk.talk_user_id,
        reason: _this.data.content,
        content: _this.data.reason_val
      },
      method: 'POST',
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: "举报成功"
          })
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})